#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>

#define SIZE 30
#define PORT 5566
#define FILENAME0 "1.xml"
#define FILENAME1 "1.jpg"
#define FILENAME2 "2.jpg"
#define FILENAME3 "2.avi"

int main(){
  int socketfd;
  int ret;

  struct stat buf_pic;
  stat(FILENAME0,&buf_pic);
  printf("file size = %d\n",buf_pic.st_size);
  int data_size0= buf_pic.st_size;
  char data_suffix0[10] = FILENAME0;
  char buffer[SIZE];
  char *buffer_temp0 = (char*)malloc(sizeof(char)*data_size0);
  FILE* fd0 = fopen(FILENAME0,"rb+");
  if(fd0<0){perror("wrong file");}
  fread(buffer_temp0,sizeof(char),data_size0,fd0);

  stat(FILENAME1,&buf_pic);
  printf("file size = %d\n",buf_pic.st_size);
  int data_size1= buf_pic.st_size;
  char data_suffix1[10] = FILENAME1;
  char *buffer_temp1 = (char*)malloc(sizeof(char)*data_size1);
  FILE* fd1 = fopen(FILENAME1,"rb+");
  if(fd1<0){perror("wrong file");}
  fread(buffer_temp1,sizeof(char),data_size1,fd1);

  stat(FILENAME2,&buf_pic);
  printf("file size = %d\n",buf_pic.st_size);
  int data_size2= buf_pic.st_size;
  char data_suffix2[10] = FILENAME2;
  char *buffer_temp2 = (char*)malloc(sizeof(char)*data_size2);
  FILE* fd2 = fopen(FILENAME2,"rb+");
  if(fd2<0){perror("wrong file");}
  fread(buffer_temp2,sizeof(char),data_size2,fd2);

  stat(FILENAME3,&buf_pic);
  printf("file size = %d\n",buf_pic.st_size);
  int data_size3= buf_pic.st_size;
  char data_suffix3[10] = FILENAME3;
  char *buffer_temp3 = (char*)malloc(sizeof(char)*data_size3);
  FILE* fd3 = fopen(FILENAME3,"rb+");
  if(fd3<0){perror("wrong file");}
  fread(buffer_temp3,sizeof(char),data_size3,fd3);

  struct sockaddr_in address;
  struct hostent* host;
  struct netent* net;

  

  socketfd = socket(AF_INET,SOCK_STREAM,0);//创建一个套接字

  address.sin_family = AF_INET;//设置为因特网协议
  address.sin_port = htons(PORT);//设置端口号
  inet_pton(AF_INET,"127.0.0.1",&address.sin_addr.s_addr);//设置ip地址

  ret=connect(socketfd,(struct sockaddr*)&address,sizeof(address));//连接服务器

  if(ret == -1){
    printf("can't connect!\n");
    exit(0);
  }
  int n=0;
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"ip");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send ip error\n");
 
  const char* address1   = "192.168.1.103";
  const char* netmask   = "255.255.0.0";
  const char* network   = "192.168.1.0";
  const char* broadcast = "192.168.1.0";
  int x;
  for(x=0;x<4;x++){
    memset(buffer,0,sizeof(buffer));
    if(x==0) 
    {
      sprintf(buffer,"%s",address1);
    }
    else if(x==1)
    {
      sprintf(buffer,"%s",netmask);
    }
    else if(x==2)
    {
      sprintf(buffer,"%s",network);
    }
    else if(x==3)
    {
      sprintf(buffer,"%s",broadcast);
    }
    printf("ip send = %s\n",buffer);
    n=send(socketfd,buffer,sizeof(buffer),0);
    if(n<0)perror("send size error\n");
  }

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
    
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"time");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send set error\n");
 
  const char* time = "\"20150604 19:54:00\"";
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"%s",time);
  printf("setting send = %s\n",buffer);
  n=send(socketfd,buffer,sizeof(buffer),0);
  if(n<0)perror("send size error\n");

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 

  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"set");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send set error\n");
 
  int setting = (1<<10)|(2<<8)|(3<<6)|(3<<4)|(2<<2)|1;
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"%d",setting);
  printf("setting send = %s\n",buffer);
  n=send(socketfd,buffer,sizeof(buffer),0);
  if(n<0)perror("send size error\n");

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  }

  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"prm_num");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send set error\n");
 
  int prm_num = 4;
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"%d",prm_num);
  printf("prm_num send = %s\n",buffer);
  n=send(socketfd,buffer,sizeof(buffer),0);
  if(n<0)perror("send size error\n");

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  }

//FILE0
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data_size");
  printf("size\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send size error\n");
 

  memset(buffer,0,sizeof(buffer));
    sprintf(buffer," %-14d%s",data_size0,data_suffix0);
    printf("data size send = %s\n",buffer);
     n=send(socketfd,buffer,sizeof(buffer),0);
    if(n<0)perror("send size error\n");

  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
   
  printf("now send data:");
  fgets(buffer,sizeof(buffer),stdin);
  
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data");
  printf("data\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send data error\n");

  int num=0;
  num = send(socketfd,buffer_temp0,data_size0,MSG_WAITALL);//把数据传给服务器
  if(num<0){perror("write data error!");}
  printf("num = %d\n",num);
  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 

//FILE1
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data_size");
  printf("size\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send size error\n");
 

  memset(buffer,0,sizeof(buffer));
    sprintf(buffer," %-14d%s",data_size1,data_suffix1);
    printf("data size send = %s\n",buffer);
     n=send(socketfd,buffer,sizeof(buffer),0);
    if(n<0)perror("send size error\n");



  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
   
  printf("now send data:");
  fgets(buffer,sizeof(buffer),stdin);
  


  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data");
  printf("data\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send data error\n");

   num=0;

  num = send(socketfd,buffer_temp1,data_size1,MSG_WAITALL);//把数据传给服务器
  if(num<0){perror("write data error!");}
  printf("num = %d\n",num);
  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 


//FILE2
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data_size");
  printf("size\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send size error\n");
 

  memset(buffer,0,sizeof(buffer));
    sprintf(buffer," %-14d%s",data_size2,data_suffix2);
    printf("data size send = %s\n",buffer);
     n=send(socketfd,buffer,sizeof(buffer),0);
    if(n<0)perror("send size error\n");



  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
   
  printf("now send data:");
  fgets(buffer,sizeof(buffer),stdin);
  


  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data");
  printf("data\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send data error\n");

   num=0;

  num = send(socketfd,buffer_temp2,data_size2,MSG_WAITALL);//把数据传给服务器
  if(num<0){perror("write data error!");}
  printf("num = %d\n",num);
  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 


//FILE3
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data_size");
  printf("size\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send size error\n");
 

  memset(buffer,0,sizeof(buffer));
    sprintf(buffer," %-14d%s",data_size3,data_suffix3);
    printf("data size send = %s\n",buffer);
     n=send(socketfd,buffer,sizeof(buffer),0);
    if(n<0)perror("send size error\n");



  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
   
  printf("now send data:");
  fgets(buffer,sizeof(buffer),stdin);
  


  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"data");
  printf("data\n");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send data error\n");

   num=0;

  num = send(socketfd,buffer_temp3,data_size3,MSG_WAITALL);//把数据传给服务器
  if(num<0){perror("write data error!");}
  printf("num = %d\n",num);
  memset(buffer,0,sizeof(buffer));
  while(1){
    n=recv(socketfd,buffer,sizeof(buffer),0);
    printf("receive ack = %s\n",buffer);
    if(strcmp(buffer,"ACK")==0)break;
  } 
  fclose(fd0);
  fclose(fd1);
  fclose(fd2);
  fclose(fd3);

  sprintf(buffer,"name DONE");
  close(socketfd);//关闭套接字
  return 0;
}

